<?php

class User extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'users':
	 * @var string $username
	 * @var string $password
	 * @var string $email
	 * @var string $status
	 * @var string $logged 
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username', 'unique'),
			array('username, password, email, status', 'required'),
			array('username, password', 'length', 'max'=>45),
			array('email', 'length', 'max'=>80),
			array('status', 'in', 'range'=>array(0,1,2)),
			array('logged', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('username, password, email, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'username' => 'Логин',
			'password' => 'Пароль',
			'email' => 'email',
			'status' => 'Статус',
			'logged' => 'Последний вход',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('username',$this->username,true);

		$criteria->compare('password',$this->password,true);

		$criteria->compare('email',$this->email,true);

		$criteria->compare('status',$this->status,true);

		$criteria->compare('logged',$this->logged,true);

		return new CActiveDataProvider('User', array(
			'criteria'=>$criteria,
		));
	}
	
	public function beforeSave(){
		if(parent::beforeSave()){
			if(empty($this->logged))
				$this->logged = null;
			
			
			return true;
		} else
			return false;
	}

	protected function uploadAvatar(){
		if($this->_avatar = CUploadedFile::getInstance($this,'_avatar')) {

			$filename = $this->username.'.'.strtolower($this->_avatar->extensionName);
			$path = Yii::getPathOfAlias( Yii::app()->params['paths']['avatar']['path'] ).DIRECTORY_SEPARATOR;

			if(	!empty($this->avatar) && 
				is_file(Yii::getPathOfAlias('webroot').$this->avatar) &&
				$this->avatar != Yii::app()->params['default']['avatar'] &&
				file_exists(Yii::getPathOfAlias('webroot').$this->avatar)
			) {
				$info = pathinfo($this->avatar);
				rename(Yii::getPathOfAlias('webroot').$this->avatar, Yii::getPathOfAlias(Yii::app()->params['paths']['avatar']['path']).DIRECTORY_SEPARATOR.$info['filename'].'.old.'.strtolower($info['extension']));
			}

			$this->_avatar->saveAs($path.$filename);
			WImage::thumbnailImage($path.$filename, 100, 100, true);
			$this->avatar = Yii::app()->params['paths']['avatar']['url'].'/'.$filename;
		} elseif(empty($this->avatar)){
			$this->avatar = null;
		}
	}
	
	protected function deleteAvatar(){
		$this->avatar = null;
	}
	
	public function updateLogged(){
		$this->logged = date('Y-m-d H:i:s');
		$this->save(false);
	}
	
	public function validatePassword($password)
	{
		return $this->hashPassword($password,Yii::app()->params['salt'])==$this->password;
    }
 
	public function hashPassword($password,$salt)
	{
		#return md5($salt.$password);
		return $password;
	}
}